<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\CustomModuleFieldAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块字段-业务类
 */
class CustomModuleFieldBusiness extends BaseBusiness
{
        #[Inject]
        protected CustomModuleFieldAccess $CustomModuleFieldAccess;

        /**
         * 创建
         * @param mixed|null $customModuleId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function create(mixed $customModuleId = null): array
        {
                $arrInsert = [
                        'customModuleFieldId' => snowflakeId(),
                        'customModuleId' => $this->request->input('customModuleId', $customModuleId),
                        'labelName' => $this->request->input('labelName'),
                        'fieldName' => $this->request->input('fieldName'),
                        'type' => $this->request->input('type'),
                        'rule' => $this->request->input('rule', '[]'),
                        'config' => $this->request->input('config', '{}'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'createTime' => $this->request->getAttribute('timestamp'),
                ];
                $bool = $this->CustomModuleFieldAccess->insert($arrInsert);
                if (!$bool) return businessError();
                $this->createOperateLog([], $arrInsert);
                return businessSuccess(['customModuleFieldId' => $arrInsert['customModuleFieldId']]);
        }

        /**
         * 更新
         * @param mixed|null $customModuleId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function update(mixed $customModuleId = null): array
        {
                $arrData = $this->CustomModuleFieldAccess->find($this->request->input('customModuleFieldId'),
                        ['customModuleId', 'labelName', 'fieldName', 'type', 'rule', 'config', 'sort', 'updateTime']);
                $arrUpdate = [
                        'customModuleFieldId' => $this->request->input('customModuleFieldId'),
                        'customModuleId' => $this->request->input('customModuleId', $customModuleId),
                        'labelName' => $this->request->input('labelName'),
                        'fieldName' => $this->request->input('fieldName'),
                        'type' => $this->request->input('type'),
                        'rule' => $this->request->input('rule', '[]'),
                        'config' => $this->request->input('config', '{}'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'updateTime' => $this->request->getAttribute('timestamp'),
                ];
                $intEffectRow = $this->CustomModuleFieldAccess->updateById($arrUpdate);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData, $arrUpdate);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->CustomModuleFieldAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 查询列
         * @param array $inject
         * @return array
         */
        public function pluck(array $inject = []): array
        {
                $arrResult = $this->CustomModuleFieldAccess->pluck($inject);
                return businessSuccess($arrResult);
        }

        /**
         * 删除
         * @param array $inject
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function delete(array $inject = []): array
        {
                $arrData = $this->CustomModuleFieldAccess->find($this->request->input('customModuleFieldId'));
                $intEffectRow = $this->CustomModuleFieldAccess->delete($inject ?: [
                        'wheres' => ['customModuleFieldId' => $this->request->input('customModuleFieldId')]
                ]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }

        /**
         * 关联性删除
         * @param mixed $customModuleId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function relevanceDelete(mixed $customModuleId): array
        {
                $arrData = $this->CustomModuleFieldAccess->forPage([
                        'wheres' => ['customModuleId' => $customModuleId], 'page' => 1, 'limit' => 10000,
                        'fields' => ['customModuleFieldId', 'customModuleId', 'labelName', 'fieldName', 'rule',
                                'config', 'sort', 'createTime', 'updateTime'],
                ]);
                $intEffectRow = $this->CustomModuleFieldAccess->delete(['wheres' => ['customModuleId' => $customModuleId]]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }
}